<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/common/common.jsp" %>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>订单列表</title>
	<link href="${ctx}/static/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/static/dist/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/static/plugins/datatables/css/matrix-style.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/static/dist/css/AdminLTE.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/static/plugins/datatables/css/dataTables.bootstrap.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/static/plugins/validationEngine/css/validationEngine.jquery.css" rel="stylesheet"
		  type="text/css">
	<link href="${ctx}/static/plugins/layer/skin/default/layer.css" rel="stylesheet" type="text/css"/>
    <style>
        button.searching{
            background-color:#666
        }
          * {
            margin: 0;
            padding: 0;
        }

        body, button, input, select, textarea {
            font: 14px/1.5 tahoma, '微软雅黑 Regular', '微软雅黑';
        "\5b8b\4f53";
            -ms-overflow-style: scrollbar;
        }

        a, a:hover {
            text-decoration: none;
        }

        .content {
            width: 100%;
            margin: auto;
        }

        .content_about, .content_list {
            width: 96%;
            margin: auto;
        }

        .content_about {
            min-height: 300px;
            padding-bottom: 60px;
            color: #666;
        }

        .order-address {
            margin-top: 90px;
            width: 700px;
        }

        .order-address h2, .order-list h2 {
            margin-bottom: 30px;
            line-height: 25px;
            color: #333;
            font-weight: 700;
            font-size: 20px;
        }

        ul, li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .address-wrap:hover {
            background-color: #fff0e8;
        }

        .order-address .address-wrap .address-list li {
            position: relative;
            line-height: 25px;
            padding-left: 92px;
            border: 1px solid #fff;
        }

        .selected {
            border-color: #f40;
            background-color: #fff0e8;
            margin: 5px 0 7px;
            line-height: 32px;
            box-shadow: 5px 5px 0 #f3f3f3;
        }

        .addressBox {
            margin-left: 10px;
            margin-bottom: 10px;
        }

        .addressInfo {
            display: inline-block;
            color: #3c3c3c;
            vertical-align: baseline;
        }

        .address-list li label em {
            color: grey;
            font-size: 14px;
            font-weight: 700;
            font-style: normal;
        }

        .btn-bar {
            margin-top: 24px;
        }

        .btn-bar .newAddrBtn {
            margin-left: 28px;
            width: 112px;
            height: 30px;
            display: inline-block;
            text-indent: 6px;
            vertical-align: text-top;
            background: #337ab7;
            line-height: 26px;
            border: 1px solid #2e6da4;
            border-radius: 6px;
            color: white;

        }

        .btn-bar .newAddrBtn:hover {
            background: #286090;
            color: white;
        }

        .addr_form {
            width: 809px;
            height: 400px;
            margin: auto;
        }

        .red {
            color: red;
        }

        .edit_btn {
            text-align: center;
            margin-top: 40px;
        }
    </style>
</head>
<body>
<div class="widget-box">
	<div class="widget-title">
	   <h5>请输入查询条件</h5>
	</div>
	<div class="widget-content nopadding">
		<!-- 用于表格显示时的标识 根据入口来传值 1标识进货单(只显示未审核) -->
		<input type="hidden" id="ide" value="${ide}"/>
		<table style="line-height:45px;font-size:12px;">
			<tr>
				<c:if test="${ide != '1'}">
				<td>&nbsp;&nbsp;&nbsp;状态：</td>
				<td>
					<select id="status" class="form-control">
						<option value="">请选择</option>
 						<!--<option value="0">待审核</option> -->
						<option value="1">待付款</option>
						<option value="2">待发货</option>
						<option value="3">已发货</option>
						<option value="4">交易完成</option>
						<option value="5">交易关闭</option>
						<option value="6">退货中</option>
						<option value="7">已退货</option>
					</select>
				</td>
				</c:if>
				<td>&nbsp;&nbsp;&nbsp;供应商：</td>
				<td>
				<select  id="supplerId" name="supplerId"  class="form-control" style="width:200px;">
					<option value="">请选择</option>
                	<c:forEach var="item" varStatus="status" items="${supplierList}">
                    	<option value="${item.id}">${item.supplierName}</option>
                    </c:forEach>
                </select>
				</td>
				
				<td>&nbsp;&nbsp;&nbsp;订单号：</td>
				<td>
					<input type="text" class="form-control" name="orderNo" id="orderNo"  style="width:200px;" placeholder="订单号" >
				</td>	
			</tr>
			<tr>
				<td>&nbsp;&nbsp;&nbsp;下单时间：</td>
				<td>
					<input name="startTimeQuery" id="startTimeQuery" class="form-control" data-type="date_00" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true})"/>
				</td>
				<td>&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</td>
				<td>
					<input name="stopTimeQuery" id="stopTimeQuery" class="form-control" data-type="date_00" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true})"/>
				</td>
				<td></td>
				<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="btn btn-success" href="javascript:void(0);" onclick="query();">查 询</a></td>			
			</tr>
		</table>
	</div>
</div>
 <div class="row">
		 <div class="col-xs-12">
		 	<div class="box">
		 		<div class="box-header">
	                 <h3 class="box-title">订单列表</h3>
	             </div>
	             <!-- 列表 -->
	             <div class="box-body table-responsive">
	             	<table id="dataTables" class="table table-bordered table-hover">
	             	</table>
	             </div>
		 	</div>
		 </div>
	</div>
<script src="${ctx}/static/plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="${ctx}/static/bootstrap/js/bootstrap.min.js"></script>
<!-- datatable -->
<script src="${ctx}/static/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
<script src="${ctx}/static/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
<script src="${ctx}/static/js/common/time/WdatePicker.js" type="text/javascript"></script>
<script src="${ctx}/static/plugins/validationEngine/jquery.validationEngine-zh_CN.js"></script>
<script src="${ctx}/static/plugins/validationEngine/jquery.validationEngine.js"></script>
<script src="${ctx}/static/plugins/layer/layer.js"></script>
<script>
   var tb;
   $(function () {
		//列表初始化
		tb = $('#dataTables').dataTable({
				"bServerSide": true,
				"destroy": true,//如果需要重新加载的时候请加上这个
				"sPaginationType": "full_numbers",
				"bJQueryUI": true,
				"bAutoWidth": true,//自动宽度
				"bSort": false, //排序功能
				"sDom": '<"">t<"F"ip>',
            "sAjaxSource": "${ctx}/api/agentOrder/getAgentOrderList", //ajax调用接口
				"aoColumnDefs": [{sDefaultContent: '',aTargets: [ '_all' ]}],
	       		"aoColumns": [
					  	{ "sTitle" : "编号","mData": "id" ,"class":"sClass"},
		                { "sTitle" : "订单号","mData": "orderNo","class":"sClass"   },
		                { "sTitle" : "供应商","mData": "supplierName","class":"sClass"   },
		                { "sTitle" : "订单商品","mData": "description","class":"sClass"   },
		                { "sTitle" : "下单时间","mData": "createTime","class":"sClass"   },
		                { "sTitle" : "联系人","mData": "contacts","class":"sClass"   },
		                { "sTitle" : "联系电话","mData": "telPhone","class":"sClass"   },
		                { "sTitle" : "商品金额","mData": "money","class":"sClass"   },
		                { "sTitle" : "物流费用","mData": "freight","class":"sClass"   },
		                { "sTitle" : "订单总额","mData": "totalCost","class":"sClass"   },
		                { "sTitle" : "状态","mData": "status","class":"sClass","mRender":function(data, type, full){
		                	var text;
		                	var status = full.status;
		                	switch(status)
		                	{
			                	case '0'://状态(0待审核，1待付款，2待发货，3已发货，4,交易完成，5交易关闭，6退货中，7已退货)
			                		text="待审核";
			                	  break;
			                	case '1':
			                		text="待付款";
			                	  break;
			                	case '2':
			                		text="待发货";
			                	  break;
			                	case '3':
			                		text="已发货";
			                	  break;
			                	case '4':
			                		text="交易完成";
			                	  break;
			                	case '5':
			                		text="交易关闭";
			                	  break;
			                	case '6':
			                		text="退货中";
			                	  break;
			                	case '7':
			                		text="已退货";
			                	  break;
			                	}
		                    	return text;
	        				}
		                }, 
		                {"sTitle" : "操作","mData" : "id", "mRender" : function ( data, type, full ) {
		                	 var text = '<div class="btn-group" style="text-align:left;"><button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">操作 <span class="caret"></span></button>';
							  text = text+'<ul class="dropdown-menu" role="menu">';
                            <%-- 							  text += '<li><a href="${ctx}/api/product/toUpdateProduct?id='+full.id+'">修改商品</a> </li>'; --%>
							//状态(0待审核，1待付款，2待发货，3已发货，4,交易完成，5交易关闭，6退货中，7已退货)
							if(full.personIde != 4){//不是平台管理员
								if(full.status == '0'){//待审核
									 text += '<li class="divider"></li>';
									 text += '<li><a href="javascript:void(0);" onclick="cancel(\''+full.id+'\');">取消</a> </li>';
								}
								if(full.status == '1'){//待付款
									  /** 1商户 2市级代理 3 非市级代理(其他) 4平台 5供应商  */
									  if(full.personIde == 2){//待审核
										  text += '<li class="divider"></li>';
                                          text += '<li><a href="${ctx}/api/forOrderPayment/agentPay?orderId=' + full.id + '" >付款</a> </li>';
									  }
									  text += '<li class="divider"></li>';
									  text += '<li><a href="javascript:void(0);" onclick="cancel(\''+full.id+'\');">取消</a> </li>';
								  }
								  if(full.status == '2'){//待发货
									  /** 1商户 2市级代理 3 非市级代理(其他) 4平台 5供应商  */
									  if(full.personIde == 2){//待审核
// 										  text += '<li class="divider"></li>';
// 										  text += '<li><a href="javascript:void(0);" >申请退款</a></li>';
									  }else if(full.personIde == 5){
										  text += '<li class="divider"></li>';
										  text += '<li><a href="javascript:void(0);" onclick="send(\''+full.id+'\');" >发货</a></li>';
									  }
								  }
								  if(full.status == '3'){//已发货
									  /** 1商户 2市级代理 3 非市级代理(其他) 4平台 5供应商  */
									  if(full.personIde == 2){//待审核
										  text += '<li class="divider"></li>';
										  text += '<li><a href="javascript:void(0);" onclick="receive(\''+full.id+'\');" >确认收货</a></li>';
									  }
								  }
							}
							if(full.personIde == 2){//市级代理(进货)
							  text += '<li class="divider"></li>';
                                text += '<li><a href="${ctx}/api/agentOrder/orderEnterDetailPage?id=' + full.id + '">详情</a> </li>';
							}else if(full.personIde == 5){//供应商(发货)
								text += '<li class="divider"></li>';
                                text += '<li><a href="${ctx}/api/agentOrder/orderSendDetailPage?id=' + full.id + '">详情</a> </li>';
							}else if(full.personIde == 4){//平台
								text += '<li class="divider"></li>';
                                text += '<li><a href="${ctx}/api/agentOrder/orderDetailPage?id=' + full.id + '">详情</a> </li>';
							}else if(full.personIde == 3){//非市级代理
								//不显示详情按钮
							}
							  text = text + '</ul></div>';
							  return text;
 							} 
		                }
	            ],
		        "oLanguage" : {
		           "sProcessing" : "数据加载中······",
		           "sLengthMenu" : "显示 _MENU_ 条记录",
		           "sZeroRecords" : "没有您要搜索的内容！",
		           "sEmptyTable" : "列表中无数据存在！",
		           "sInfo" : "当前显示 _START_ 到 _END_ 条数据，共 _TOTAL_ 条数据",
		           "sInfoEmpty" : "显示 0 到 0 条记录",
		           "sInfoFiltered" : "数据列表中共  _MAX_ 条记录",
		           "oPaginate" : {
		               "sFirst" : "首页",
		               "sPrevious" : "上一页",
		               "sNext" : "下一页",
		               "sLast" : "末页"
		           }
		        },
		        "fnServerData" : function ( sSource, aoData, fnCallback, oSettings ) {
					oSettings.jqXHR = $.ajax( {
						"dataType" 	: 'json',
						"type" 		: "GET",
						"async"     : 'false',
						"url" 		: sSource,
						"data" 		: {
							'pageNum': (aoData[3].value/aoData[4].value)+1,
							'pageSize' :  aoData[4].value,
							'ide'  :  $("#ide").val(),
							'status'  :  $("#status").val(),
							'supplerId'  :  $("#supplerId").val(),
							'orderNo'  :  $.trim($("#orderNo").val()),
							'startTimeQuery'  :  $("#startTimeQuery").val(),
							'stopTimeQuery'  :  $("#stopTimeQuery").val()
						}, 
						"success"	: fnCallback
					} );
				},
		  });
		//classify(0);
		//supplier();
		
		$("#cancel").click(function () {
		    close();
		});
		
		 $("#shippingForm").validationEngine({ 
				promptPosition: "topRight", 
				validationEventTriggers:"keyup blur", 
				success :  false,
				failure : function() { callFailFunction()  }
			});
		
	});
   
	//条件查询重新加载数据
	function query() {
	 tb.fnDraw();
	};

	//取消订单 by yyg
	function cancel(id){
		console.log(id);
		layer.confirm('确定取消订单?', {icon: 3, title:'提示',closeBtn:0}, function(index){
			$.ajax({ 
				url:"/api/forAgentOrder/chargeBack",
				data:"orderId="+id,
				datetype:"json",
				type:"post",
				 success:function(Res){
				    	if(Res.code == '200'){
				    		tb.fnDraw();
				    		layer.alert('取消订单成功', {icon: 1,closeBtn: 0}); 
				    	}else if(Res.code == '80009'){
				    		layer.alert('取消订单失败', {icon: 2,closeBtn: 0}); 
				    	}
				    },error:function(Res){
				    	layer.alert('与服务器连接出错', {icon: 2,closeBtn: 0}); 
				    }
		      });	
			layer.close(index);
		});
	}
	//发货
	
	function send(id){
// 		 $("#logisticsId").append("<option value=''>请选择</option>");
// 		 $("#sendAddressId").append("<option value=''>请选择</option>");
			//弹出隐藏窗口的时候先查询相关数据
			//获取物流列表
				$.ajax({ 
				url:"/api/forOrder/getLogistics",
				datetype:"json",
				type:"post",
				 success:function(d){
					
						for(var i=0; i<d.data.length;i++){
				           $("#logisticsId").append("<option value='"+d.data[i].id+"'>"+d.data[i].logisticsName+"</option>");
				     }
				 }
		      });	
				//获取发货地址列表
				$.ajax({ 
					url:"/api/forOrder/shippingAddress",
					datetype:"json",
					type:"post",
					 success:function(data){
							 for(var i=0; i<data.data.length;i++){
									console.log(data.data[i]);
					              $("#sendAddressId").append("<option value='"+data.data[i].id+"'>"+data.data[i].provinceName+""+data.data[i].cityName+""+data.data[i].areaName +""+data.data[i].address+"</option>");
					    	}
					 }
			   });
				sendDiv = layer.open({
				        title: "订单发货",
				        type: 1,
				        closeBtn: 0,
				        resize: false,
// 				        skin: 'layui-layer-rim', //加上边框
				        area: ['800px', '400px'], //宽高
				        content: $("#addForm")
				    });
		console.log(id);
		$("#id").val(id);
	}
	function  close(){
			resetForm();
			layer.close(sendDiv);
		
	}
	
		function  resetForm(){
			$("#shippingForm").find("input").val("");
			$("#sendAddressId").find("option").remove();
		    $("#logisticsId").find("option").remove();
		}
		
		
		function  save(){
			//表单验证
		 	var flag = $("#shippingForm").validationEngine("validate");
	    	if(!flag){
	     		return false;
	    	} 
			$.ajax({ 
				url:"/api/forAgentOrder/Shipping",
				data:$("#shippingForm").serialize(),
				datetype:"json",
				type:"post",
				 success:function(Res){
				    	if(Res.code == '200'){
				    		tb.fnDraw();
				    		close();
				    		layer.alert('发货成功', {icon: 1,closeBtn: 0}); 
				    	}else if(Res.code == '800011'){
				    		layer.alert('发货失败', {icon: 2,closeBtn: 0}); 
				    	}
				    },error:function(Res){
				    	layer.alert('与服务器连接出错', {icon: 2,closeBtn: 0}); 
				    }
		      });	
		}
		
	//确认收货
	function receive(id){
		layer.confirm('确认已收到货品了吗?',{
            icon: 1,
            btn: ['确认收货', '取消'],
            btn1: function(index){
                $.ajax({
                    url: "${ctx}/api/forOrderPayment/agentConfirmReceipt",
    				data: {orderId : id},
    				datetype:"json",
    				type:"post",
    				 success:function(data){
    					 	layer.close(index);
    				    	if(data.code == 200){
    				    		layer.msg('成功',{icon: 1, time: 2000},
    		       						function(index){
    				    			tb.fnDraw();
    		   		            });
    				    	}else{
    				    		layer.msg('失败',{icon: 2, time: 2000});
    				    	}
    				    },error:function(Res){
    				    	layer.alert('与服务器连接出错', {icon: 2,closeBtn: 0}); 
    				    }
    		      });
            },
            btn2: function(index){
            	layer.close(index);
            }
        });
	}
</script>
<!--   收货地址弹出层开始 by  yyg -->
  
<div class="addr_form" id="addForm" style="display:none;">
 <form class="form-horizontal"  id="shippingForm" style="margin-top: 40px;">
        <div class="form-group">
        <label for="" class="col-sm-1 control-label"><span class="red">*</span>发货地址:</label>
            <div class="col-sm-3">
                <select id="sendAddressId" name="sendAddressId" class="form-control validate[required]">
	                    	</select>
	           </div>
        </div>
        <div class="form-group">
            <label for="" class="col-sm-1 control-label"><span class="red">*</span>物流公司:</label>
            <div class="col-sm-3">
                <select id="logisticsId" name="logisticsId" class="form-control validate[required]">
	                    	
	                    	</select>
	                    	</div>
        	</div>
        	
        <div class="form-group">
            <label for="phone" class="col-sm-1 control-label"><span class="red">*</span>物流编号:</label>
            <div class="col-sm-4">
                <input type="text" class="form-control validate[required]"    maxlength="50" id="logisticsNo"    name="logisticsNo" placeholder="物流编号" />
            </div>
      </div>
          <div class="form-group">
            <div class="edit_btn">
                <button type="button" class="btn btn-primary"    onclick="save();"  id="addAddress">发货</button>
                <button type="button" class="btn btn-danger"   id="cancel">取消</button>
            </div>
            </div>
	<!--   隐藏域参数 by  yyg -->
         	<input type="hidden"   id="id"  value=""   name="id"  />
  <!--   隐藏域参数 by  yyg --> 
  </form>
          </div>
<!--   收货地址弹出层结束 by  yyg -->	
</body>
</html>